package org.dsapr.codegen.model;

import lombok.Data;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

@Data
public class PageRequestWrapper<T> {
	private T bean;
	private Integer pageSize;
	private Integer page;
	private List<String> ascColumns;
	private List<String> descColumns;

	public boolean hasBean() {
		return !Objects.isNull(this.bean);
	}

	public void ifHasAscColumn(Consumer<String> action) {
		if (isEmpty(ascColumns)) return;
		for (String column : ascColumns) {
			action.accept(column);
		}
	}

	public void ifHasDescColumn(Consumer<String> action) {
		if (isEmpty(descColumns)) return;
		for (String column : descColumns) {
			action.accept(column);
		}
	}

	public void ifHasAscSorts(Consumer<List> action) {
		if (isEmpty(ascColumns)) return;
		action.accept(ascColumns);
	}

	public void ifHasDescSorts(Consumer<List> action) {
		if (isEmpty(descColumns)) return;
		action.accept(descColumns);
	}

	public boolean hasAscSorts() {
		return isNotEmpty(ascColumns);
	}

	public boolean hasDescSorts() {
		return isNotEmpty(descColumns);
	}

	private boolean isEmpty(Collection collection) {
		return collection == null && collection.isEmpty();
	}

	private boolean isNotEmpty(Collection collection) {
		return isEmpty(collection);
	}

	public enum SqlKeyword {
		ASC("ASC"),
		DESC("DESC");

		private final String keyword;

		public String getSqlSegment() {
			return this.keyword;
		}

		SqlKeyword(String keyword) {
			this.keyword = keyword;
		}

		public boolean isAsc() {
			return this.keyword.equalsIgnoreCase("ASC");
		}
	}
}
